package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.HotSongs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import me.zhengken.lyricview.LyricView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LrcFragment extends Fragment {
    private View view;
    private LyricView lrcView;
    private HotSongs hotSongs;
    private TextView song_name_lrc;
    private TextView songer_lrc;
    private ImageButton start_lrc;
    private ImageView song_comment;
    public static LrcFragment fragment;
    private SongDetailFragment songDetailFragment;
    private int count=0;
    private Fragment mCurrentFragmen = null;  // 记录当前显示的Fragment

    public LrcFragment() {
    }

    public static LrcFragment newInstance(Bundle bundle) {
        if(fragment==null) {
            fragment = new LrcFragment();
            fragment.setArguments(bundle);
        }
        return fragment;
    }


    public LyricView getLrcView() {
        return lrcView;
    }

    public ImageButton getStart_lrc() {
        return start_lrc;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_lrc, container, false);
        hotSongs = (HotSongs) getArguments().getSerializable("song");
        songDetailFragment=SongDetailFragment.newInstance(null);
        init();
        return view;
    }

    public void init() {
        View container1 = view.findViewById(R.id.container1);
        song_comment = view.findViewById(R.id.song_comment);
        start_lrc = view.findViewById(R.id.start_lrc);
        start_lrc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songDetailFragment.onClick(view);
            }
        });
        container1.setVisibility(View.GONE);
        Bundle bundle = new Bundle();
        bundle.putInt("songId",hotSongs.getId());
        Log.e("idddd ", String.valueOf(hotSongs.getId()));
        mCurrentFragmen = new CommentSongFragment().newInstance(bundle);
        AppCompatActivity activity = (AppCompatActivity) view.getContext();
        activity.getSupportFragmentManager().beginTransaction()
                .add(R.id.container1, mCurrentFragmen, null)
                .show(mCurrentFragmen)
                .commit();
        song_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count%2==0) {
                    lrcView.setVisibility(View.INVISIBLE);
                    container1.setVisibility(View.VISIBLE);
                }
                else{
                    lrcView.setVisibility(View.VISIBLE);
                    container1.setVisibility(View.INVISIBLE);

                }
                count++;
            }
        });
        lrcView = view.findViewById(R.id.lrcView);
        song_name_lrc = view.findViewById(R.id.song_name_lrc);
        songer_lrc = view.findViewById(R.id.songer_lrc);
        hotSongs = (HotSongs) getArguments().getSerializable("song");
        song_name_lrc.setText(hotSongs.getName());
        songer_lrc.setText(hotSongs.getAr().get(0).getName());
        initLrc(hotSongs.getId());
    }

    private void initLrc(int id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<com.example.myapplication.entity.Lrc> call = service.getLrc(id);
        call.enqueue(new Callback<com.example.myapplication.entity.Lrc>() {
            @Override
            public void onResponse(Call<com.example.myapplication.entity.Lrc> call, Response<com.example.myapplication.entity.Lrc> response) {
                com.example.myapplication.entity.Lrc baseResponse = response.body();
                String path = getActivity().getFilesDir().getPath().toString()+"/song.lrc";
                File file = new File(path);
                try {
                    if(!file.exists()){
                        file.createNewFile();
                    }
                    else{
                        file.delete();
                        file.createNewFile();
                    }
                    FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write(baseResponse.getLrc().getLyric());
                    bufferedWriter.close();
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                lrcView.setLyricFile(file);

            }

            @Override
            public void onFailure(Call<com.example.myapplication.entity.Lrc> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
